package com.zjdiepu.www.rest.api.system;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.ibiz.base.ISysUserService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.base.request.ChangePassword;
import com.zjdiepu.www.util.PasswordUtil;

/**
 * 项目名称：diepu-parent-rest <br/> 
 * 类名称：SysUserController <br/>  
 * 类描述：用户管理 <br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年6月5日 上午11:40:39  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年6月5日 上午11:40:39   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value = "SysUserController", produces="application/json;charset=UTF-8",method = RequestMethod.POST)
@Api(tags = {"用户管理"}, produces="application/json;charset=UTF-8")
public class SysUserController extends BaseController {
	@Resource
	private TokenUserHelper tokenUserHelper;
	@Resource
	private ISysUserService sysUserService;
	
	/**
	 * 修改密码
	 * @author xiongxiaotun
	 * @date 2017年3月21日
	 * @since v1.0.0
	 * @param changePassword
	 * @return
	 */
	@ApiOperation(value = "修改密码", response = Boolean.class, httpMethod = "POST")
	@ApiImplicitParams({
			@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue = "ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
			@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue = "Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String") })
	@RequestMapping(value = { "change" }, method = { RequestMethod.POST })
	public ApiResponse change(@Validated @RequestBody ChangePassword changePassword) {
		SysUser currentUser = (SysUser)tokenUserHelper.getUser();
		if(!changePassword.getEnsurePassword().equals(changePassword.getNewPassword())) {
			return ApiResponse.fail(ResultFlagType.ENSURE_PASSWORD_DIFFERENT);
		}
		if(!currentUser.getLoginPass().equals(PasswordUtil.getDbPasswordForMd5(currentUser.getLoginName(), changePassword.getOldPassword()))){
			return ApiResponse.fail(ResultFlagType.OLD_PASSWORD_WRONG);
		}
		currentUser.setLoginPass(PasswordUtil.getDbPasswordForMd5(currentUser.getLoginName(), changePassword.getNewPassword()));
		sysUserService.saveUpdateAndDelete(false, currentUser, currentUser, OperateType.edit);
		/**
		 * 清除状态，重新登录
		 */
		tokenUserHelper.delUser();
		return ApiResponse.success(true);
	}
}
